package com.flyan.game.motafx.core.event;

import cn.hutool.json.JSONObject;
import com.flyan.game.motafx.core.util.script.GroovyScriptUtils;
import javafx.util.Duration;
import lombok.Setter;

/**
 * 基础事件
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/10
 */
public abstract class BaseEvent implements Event {

    /**
     * 事件持续时间
     */
    @Setter
    protected Duration duration;

    /**
     * 属性
     */
    @Setter
    protected JSONObject properties;

    /**
     * 事件脚本
     */
    @Setter
    protected String script;

    @Override
    public Duration duration() {
        return duration != null ? duration : Duration.ZERO;
    }

    @Override
    public JSONObject properties() {
        return properties;
    }

    @Override
    public void occur() {
        executeScript();
    }

    public void executeScript() {
        /* 有自定义脚本，执行 */
        if (script != null) {
            GroovyScriptUtils.invokeScript(script, "execute", this);
        }
    }

}
